package cn.alise.qdp.service.user;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.alise.enums.EnumClass;
import cn.alise.exception.ServiceException;
import cn.alise.qdp.mapper.user.UserMapper;
import cn.alise.qdp.mapper.user.UserRoleMapper;
import cn.alise.qdp.model.user.User;
import cn.alise.qdp.model.user.UserRole;
import cn.alise.util.Md5;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {

	@Resource(name = "userMapper")
	private UserMapper userMapper;
	
	@Resource(name = "userRoleMapper")
	private UserRoleMapper userRoleMapper;
	
	public User get(User args) {
		List<User> list = this.list(args);
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		
		return null;
	}
	
	public User getLoginInfo(User args) {
		List<User> list = this.userMapper.getLoginInfo(args);
		
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		
		return null;
	}
	
	public List<User> gets(Map<String, Object> args){
		return this.userMapper.gets(args);
	}
	
	public List<User> list(User args) {
		return this.userMapper.list(args);
	}
	
	public List<User> page(Map<String, Object> args) {
		return this.userMapper.page(args);
	}
	
	public List<User> querypage(Map<String, Object> args) {
		return this.userMapper.querypage(args);
	}
	
	public int total(User args) {
		return this.userMapper.total(args);
	}
	
	public int querytotal(User args) {
		return this.userMapper.querytotal(args);
	}
	
	public int delete(User args) {
		return this.userMapper.delete(args);
	}
	
	public int save(User args, String roleIds) {
		// 保存用户信息
		int row = this.userMapper.insert(args);
		
		// 保存用户角色表关系
		// 拆分角色串
		String[] roleIdArr = roleIds.split(",");
		for(int i=0; i<roleIdArr.length; i++) {
			
			UserRole userRole = new UserRole();
			userRole.setUserId(args.getUserId());
			userRole.setRoleId(Long.parseLong(roleIdArr[i]));
			userRole.setCreatedTime(args.getCreatedTime());
			
			row = this.userRoleMapper.insert(userRole);
		}
		
		return row;
	}
	
	public int updateUserInfo(User args, String roleIds) {
		// 更新用户信息
		int row = this.userMapper.update(args);
		
		// 保存用户角色表关系
		// 先删除用户角色关系
		UserRole userRoleDel = new UserRole();
		userRoleDel.setUserId(args.getUserId());
		this.userRoleMapper.delete(userRoleDel);
		
		// 再保存用户角色关系
		// 拆分角色串
		String[] roleIdArr = roleIds.split(",");
		for(int i=0; i<roleIdArr.length; i++) {
			
			UserRole userRole = new UserRole();
			userRole.setUserId(args.getUserId());
			userRole.setRoleId(Long.parseLong(roleIdArr[i]));
			userRole.setUpdatedTime(args.getUpdatedTime());
			
			row = this.userRoleMapper.insert(userRole);
		}
		
		return row;
	}
	
	public int update(User args) {
		return this.userMapper.update(args);
	}
	
	public List<User> judgeUserByOrganId(User args) {
		return this.userMapper.judgeUserByOrganId(args);
	}
	
	public List<User> authori(User args) {
		return this.userMapper.authori(args);
	}
	/*
	 * 该方法用于找到对应账号的照片路径
	 */
	public String getPath(User userAccount) {
		return this.userMapper.getPath(userAccount);
	}
	
	public Map<String,Object> accountverification(Map<String, Object> params) throws ServiceException{
	
		String account = params.get("account").toString();
		String password = params.get("password").toString();
		User entity = new User();
		entity.setAccount(account);
		entity = this.get(entity);
		Map<String,Object> map = new HashMap<String,Object>();
		
		//验证用户账号的有效性
		if(entity == null) {
			map.put("code", "1");
			map.put("msg", "用户账号或密码错误！");
			return map;
		}
		
		//验证用户密码的有效性 
		String pwd = Md5.md5(Md5.md5(password) + entity.getMask().toString());
		if (!pwd.equals(entity.getPassword())) {
			map.put("code", "2");
			map.put("msg", "用户账号或密码错误！");
			return map;
		}
		
		//验证用户账号状态是否正确
		if(entity.getStatus() != EnumClass.UserStatus.VALID.getKey()) {
			map.put("code", "3");
			map.put("msg", "当前用户状态不对！");
			return map;
		}
		
		//修改账户登录信息
		User newaccount = new User();
		newaccount.setUserId(entity.getUserId());
		newaccount.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
		
		int row = this.update(newaccount);
		if (row == 1) {
			if (entity != null) {
				entity.setPassword("");
				entity.setMask("");
			}
			map.put("user", entity);
			map.put("code", "0");
			map.put("msg", "登陆成功！");
			return map;
		} else {
			map.put("code", "4");
			map.put("msg", "登陆失败！");
			return map;
		}
				
	}
}
